//
//  CADisplayLinkViewController.m
//  MyProject
//
//  Created by yun on 2018/7/24.
//  Copyright © 2018年 yun. All rights reserved.
//

#import "CADisplayLinkViewController.h"

@interface CADisplayLinkViewController ()

@property(nonatomic,strong) CADisplayLink *playLink;
@property(nonatomic,assign) NSInteger count;

@property(nonatomic,strong) dispatch_source_t timer;


@end

@implementation CADisplayLinkViewController

- (void)dealloc{
    NSLog(@"%s",__func__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    _count = 1;
//    self.playLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(playLinkShow)];
//    self.playLink.frameInterval = 59;//  1~60，超过60速度变快
//    [self.playLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:(NSRunLoopCommonModes)];
    [self gcdTimer];
}

- (void)gcdTimer{
    /**
     * 创建新的调度源以监视低级系统对象，并自动将处理程序块提交到调度队列以响应事件。必须设置属性，否则直接回收
     type:调度源的类型
     DISPATCH_SOURCE_TYPE_DATA_ADD
     DISPATCH_SOURCE_TYPE_DATA_OR
     DISPATCH_SOURCE_TYPE_MACH_RECV
     DISPATCH_SOURCE_TYPE_MACH_SEND
     DISPATCH_SOURCE_TYPE_PROC
     DISPATCH_SOURCE_TYPE_READ
     DISPATCH_SOURCE_TYPE_SIGNAL
     DISPATCH_SOURCE_TYPE_TIMER
     DISPATCH_SOURCE_TYPE_VNODE
     DISPATCH_SOURCE_TYPE_WRITE
     DISPATCH_SOURCE_TYPE_MEMORYPRESSURE
     
     handle、mask由调度源的类型提供
     queue:队列
     */
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);//延迟
    dispatch_source_set_event_handler(_timer, ^{
        NSLog(@"hahah");
    });
    dispatch_resume(_timer);
    
}

- (void)playLinkShow{
    NSLog(@"%ld",(long)_count);
    _count++;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    dispatch_cancel(_timer);
//    dispatch_suspend(_timer);// 悬挂的源直接退出会奔溃
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
